//
//  Circle.m
//  Project 5
//
//  Created by Nguyen Tan Loc on 6/20/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "Circle.h"

@implementation Circle
@synthesize _angle = mAngle;

-(id) initWithAngle:(float)angle listSegment:(NSArray *)listSegment
{
    if (self = [super init])
    {
        self._angle = angle;
        mIsDestroy = NO;
        mBatchNode = [CCSpriteBatchNode batchNodeWithFile:BATCH_CIRCLE_SEGMENT_PNG];
        [self addChild:mBatchNode];
        
        CCSprite *sprite;
        for (int i = 0; i < listSegment.count; i++)
        {
            sprite = (CircleSegment *)[listSegment objectAtIndex:i];
            [mBatchNode addChild:sprite z:0 tag:i];
        }
    }
    
    return self;
}



-(void) runEffect:(EFFECT_TYPE)effect
{
    if (effect == EFFECT_APPEAR)
    {
        for (int i = 0; i < mBatchNode.children.count; i++)
        {
            CCSprite *sprite = (CCSprite *)[mBatchNode getChildByTag:i];
            [sprite setOpacity:0];
            
            CCAction *fadeTo = [CCFadeTo actionWithDuration:TIME_APPEAR opacity:255];
            [sprite runAction:fadeTo];
        }
    }
    else if (effect == EFFECT_DISAPPEAR)
    {
//        for (int i = 0; i < mBatchNode.children.count; i++)
//        {
//            CircleSegment *segment = (CircleSegment *)[mBatchNode getChildByTag:i];
//            [segment setOpacity:255];
//            
//            CCAction *fadeTo = [CCFadeTo actionWithDuration:TIME_DISAPPEAR opacity:0];
//            [segment runAction:fadeTo];
//        }
    }
}

-(BOOL) isDestroy
{
    if (mIsDestroy == YES) return mIsDestroy;
    
    mIsDestroy = YES;
    for (int i = 0; i < mBatchNode.children.count; i++)
    {
        CircleSegment *segment = (CircleSegment *)[mBatchNode getChildByTag:i];
        if ([segment isDestroy] == NO)
        {
            mIsDestroy = NO;
            break;
        }
    }
    return mIsDestroy;
}

-(void) setCheckCollision:(BOOL)b
{
    for (int i = 0; i < mBatchNode.children.count; i++)
    {
        CircleSegment *segment = (CircleSegment *)[mBatchNode getChildByTag:i];
        [segment setCheckCollision:b];
    }
}

-(void) contactWithBulletType:(BULLET_TYPE)type
{
//    CCLOG(@"test contact with bullet type");
}

-(CircleSegment *) getCurrentSegment
{
    for (int i = 0; i < mBatchNode.children.count; i++)
    {
        CircleSegment *segment = (CircleSegment *)[mBatchNode getChildByTag:i];
        if ([segment isDestroy]) continue;
        return segment;
    }
    
    return nil;
}

-(void) dealloc
{
    [super dealloc];
    
}

@end
